import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_lang/flutter_lang.dart';
import 'package:flutter_lifecycle_base/tab/base_tab_page_widget.dart';
import 'package:flutter_lifecycle_base/widget/status_bar_wrap.dart';
import 'package:route_manager/wx_navigator.dart';
import 'package:wallet_home/image_r.dart';
import 'package:wallet_home/module_config.dart';
import 'package:wallet_home/page/theme/colors.dart';
import 'package:wallet_home/tools/common.dart';
import 'package:wallet_home/tools/theme_utils.dart';
import 'package:wallet_home/tools/utils.dart';

// enum WalletSource {
//   // 创建
//   create,
//   // 恢复
//   recover,
//   // 预留
//   defluat,
// }

class MainPageMyWidget extends BaseTabPageWidget {
  @override
  _MainPageMyWidgetState createState() => _MainPageMyWidgetState();
}

class _MainPageMyWidgetState extends BaseTabPageState<MainPageMyWidget> {
  bool isBackUpMnemonic = false;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    isBackUpMnemonic = SpUtil.getBool("verified_mnemonic", defValue: false);
    return Scaffold(
        backgroundColor: ThemeUtils.getThemeColor(
            context: context,
            brightnessColor: Colors.grey[200],
            darkColor: Colours.dark_bg_color),
        body: SafeArea(
            child: StatusBarWrap(
                child: Container(
          //color: Colors.grey[200],
          child: Column(
            children: [
              _minePageHeader(context),
              _minePageAddress(context),
              _minePageBasicSetting(context),
              _minePageSafeSetting(context)
            ],
          ),
        ))));
  }

  /// 头部布局
  _minePageHeader(BuildContext context) {
    return Container(
      height: 180,
      width: double.infinity,
      decoration: BoxDecoration(
        gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: ThemeUtils.isDark(context)
                ? [Colors.grey[900], Colors.grey[800]]
                : [Colours.dark_app_main, Colours.app_main]),
      ),
      child: Stack(
        alignment: const FractionalOffset(0.5, 0.5),
        children: [
          Image(
            image: AssetImage(ImageR.imagesHeadDefaultIcon,
                package: ModuleConfig.moduleName),
            width: 60,
            height: 60,
            color: Colors.grey[100],
            fit: BoxFit.cover,
          ),
          Positioned(
            child: Image(
              image: AssetImage(ImageR.imagesMineLogoIcon,
                  package: ModuleConfig.moduleName),
              width: 130,
              height: 130,
              color: Colors.white,
              fit: BoxFit.fill,
            ),
            right: 1,
            top: 0,
            bottom: 20,
          ),
        ],
      ),
    );
  }

  /// 地址簿布局
  _minePageAddress(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 20),
      child: _buildMineItem(context,
          image: ImageR.imagesMineAddressIcon,
          title:
              "${WXLang.$t("home_address_book", package: ModuleConfig.moduleName)}",
          onTap: () {
        WXNavigator.pushNamed(routeName: "/addressBookPage");
      }),
    );
  }

  ///基本设置
  _minePageBasicSetting(BuildContext context) {
    return Column(
      children: [
        Container(
          height: 35,
          width: double.infinity,
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(left: 15),
          child: Text(
            "${WXLang.$t("home_system_setup", package: ModuleConfig.moduleName)}",
            style: TextStyle(
                color: ThemeUtils.getThemeColor(
                    context: context,
                    darkColor: Colors.white,
                    brightnessColor: Colors.grey[600]),
                fontSize: 12),
          ),
        ),
        Container(
          color: Colors.white,
          child: Column(
            children: [
              _buildMineItem(context,
                  title:
                      "${WXLang.$t("home_language_selection", package: ModuleConfig.moduleName)}",
                  content: _getLangModelText(),
                  onTap: () => WXNavigator.pushNamed(routeName: "/lang_page")),
//              Container(
//                height: 0.5,
//                width: double.infinity,
//                color: Colors.grey[300],
//                margin: EdgeInsets.only(left: 15),
//              ),
              Divider(
                height: 0.2,
                //color: Colors.grey[300],
              ),
              _buildMineItem(context,
                  title:
                      "${WXLang.$t("home_theme_selection", package: ModuleConfig.moduleName)}",
                  content: getThemeModelText(),
                  onTap: () => WXNavigator.pushNamed(routeName: "/theme_page")),
            ],
          ),
        )
      ],
    );
  }

  String getThemeModelText() {
    String themeType = SpUtil.getString(Constant.themeModel);
    print("你妹的======>$themeType");
    String text;
    switch (themeType) {
      case "light":
        text =
            "${WXLang.$t("home_day_mode", package: ModuleConfig.moduleName)} ";
        break;
      case "dark":
        text =
            "${WXLang.$t("home_night_mode", package: ModuleConfig.moduleName)} ";
        break;
      default:
        text =
            "${WXLang.$t("home_follow_system", package: ModuleConfig.moduleName)} ";
        break;
    }
    return text;
  }

  String _getLangModelText() {
    String langType = SpUtil.getString(Constant.langModel);
    print("语言切换======>$langType");
    String text;
    switch (langType) {
      case "zh":
        text =
            "${WXLang.$t("home_language_chinese", package: ModuleConfig.moduleName)} ";
        break;
      case "en":
        text =
            "${WXLang.$t("home_language_english", package: ModuleConfig.moduleName)} ";
        break;
      default:
        text =
            "${WXLang.$t("home_language_system", package: ModuleConfig.moduleName)} ";
        break;
    }
    return text;
  }

  ///安全设置
  _minePageSafeSetting(BuildContext context) {
    return Column(
      children: [
        Container(
          height: 35,
          width: double.infinity,
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(left: 15),
          child: Text(
            "${WXLang.$t("home_security_settings", package: ModuleConfig.moduleName)}",
            style: TextStyle(
                color: ThemeUtils.getThemeColor(
                    context: context,
                    darkColor: Colors.white,
                    brightnessColor: Colors.grey[600]),
                fontSize: 12),
          ),
        ),
        Container(
          color: Colors.white,
          child: Column(
            children: [
              _buildMineItem(context,
                  title:
                      "${WXLang.$t("home_modify_pin_code", package: ModuleConfig.moduleName)}",
                  onTap: () {
                WXNavigator.pushNamed(
                    routeName: "/checkPINPage",
                    context: context,
                    moduleName: "wallet_authentication",
                    arguments: {
                      'isHiddenBack': false,
                      'onCallBack': (String value) {
                        WXNavigator().popAndPushNamed("/pinSetting",
                            moduleName: "wallet_authentication",
                            arguments: {'isUpdate': true, 'walletSource': 0});
                      }
                    });
              }),
              Divider(
                height: 0.2,
                //color: Colors.grey[300],
              ),
              _buildMineItem(context,
                  title:
                      "${WXLang.$t("home_backup_mnemonic", package: ModuleConfig.moduleName)}",
                  content: isBackUpMnemonic
                      ? "${WXLang.$t("home_already_backup", package: ModuleConfig.moduleName)}"
                      : "${WXLang.$t("home_not_backup", package: ModuleConfig.moduleName)}",
                  onTap: () {
                if (isBackUpMnemonic) return;
                WXNavigator.pushNamed(
                    context: context,
                    routeName: '/checkPINPage',
                    moduleName: "wallet_authentication",
                    arguments: {
                      'isHiddenBack': false,
                      'onCallBack': (String value) {
                        WXNavigator().popAndPushNamed("/mnemonicBackup",
                            context: context,
                            moduleName: "wallet_authentication",
                            arguments: 1);
                      }
                    });
              }, isShowRightIcon: isBackUpMnemonic ? false : true)
            ],
          ),
        )
      ],
    );
  }

  _buildMineItem(BuildContext context,
      {String image, String title, String content, GestureTapCallback onTap, bool isShowRightIcon = true}) {
    return GestureDetector(
        onTap: onTap,
        child: Container(
            height: 45,
            width: double.infinity,
            color: ThemeUtils.getThemeColor(
                context: context,
                brightnessColor: Colors.white,
                darkColor: Colors.grey[800]),
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.only(left: image != null ? 15 : 5),
            child: Row(
              children: [
                image != null
                    ? Image(
                        image: AssetImage(ImageR.imagesMineAddressIcon,
                            package: ModuleConfig.moduleName),
                        width: 20,
                        height: 20,
                        fit: BoxFit.cover,
                        color: ThemeUtils.getThemeColor(
                            context: context,
                            darkColor: Colors.white,
                            brightnessColor: Colors.grey[700]),
                      )
                    : SizedBox(),
                Padding(
                  padding: EdgeInsets.only(left: 10),
                  child: Text(
                    title,
                    style: TextStyle(
                        color: ThemeUtils.getThemeColor(
                            context: context,
                            darkColor: Colors.white,
                            brightnessColor: Colors.grey[800]),
                        fontSize: 14),
                  ),
                ),
                Spacer(),
                Text(
                  content != null ? content : "",
                  style: TextStyle(
                      color: ThemeUtils.getThemeColor(
                          context: context,
                          darkColor: Colors.white,
                          brightnessColor: Colors.grey[600]),
                      fontSize: 14),
                ),
                Padding(
                  padding: isShowRightIcon
                      ? EdgeInsets.only(right: 15, left: 10) : EdgeInsets.only(right: 6, left: 10),
                  child: isShowRightIcon
                      ? Image(
                          image:
                              AssetImage(ImageR.imagesRightIcon, package: ModuleConfig.moduleName),
                          width: 8,
                          height: 10,
                          fit: BoxFit.fill,
                          color: ThemeUtils.getThemeColor(
                              context: context,
                              darkColor: Colors.white,
                              brightnessColor: Colors.grey[800]),
                          filterQuality: FilterQuality.high,
                        )
                      : Container(),
                ),
              ],
            )));
  }

  @override
  int getIndex() {
    return MainMyPageIndex;
  }

  @override
  void didAppear() {
    super.didAppear();
    print("我的页面回来了");
    setState(() {});
  }

  @override
  void disappear() {
    super.disappear();
    print("我的页面消失了");
  }
}
